/***************************************************************************
 *   Copyright (C) 2008/2009 by Philipp Müller                             *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QPainter>

#include "vdr.h"
#include "qtvomp.h"
#include "epgdelegate.h"

EpgDelegate::EpgDelegate ( MainWindow* mainWindow, WidgetEpgHeader* epgHeader, QObject * parent ) : QItemDelegate ( parent ),
    m_mainWindow( mainWindow ),
    m_epgHeader( epgHeader )
{
}

void EpgDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    QString channelNumber  = index.model()->data( index, ChannelListModel::ChannelNumberRole ).toString();
    QString channelName    = index.model()->data( index, ChannelListModel::ChannelNameRole ).toString();
    QIcon icon             = index.model()->data( index, Qt::DecorationRole ).value<QIcon>();

    //Trigger the epg update
    index.model()->data( index, ChannelListModel::ChannelEpgTriggerUpdate );
    ScheduleList scheduleList = m_mainWindow->channelList()->at( index.row() ).scheduleList;

    painter->save();

    int x = option.rect.left();
    int y = option.rect.top();
    int width = option.rect.width();
    int height = option.rect.height();

    QRect rect;

    //Icon 64x48
    icon.paint( painter, QRect( x, y, 64, 48 ) );

    //Channel
    painter->setFont( QFont( QString(), 12 ) );

    rect = QRect( x + 70, y + 6, 120, 22 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignTop, channelName );

    //Channel number
    rect = QRect( x + 70, y + 30, 120, height - 32 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, channelNumber );

    QRect epgRect = QRect( x + m_epgHeader->logoSpace(), y, width - m_epgHeader->logoSpace() - 10, 48 );;

    bool invertBackground = true;

    QDateTime visibleTime   = m_epgHeader->epgVisibleTime();
    QDateTime selectionTime = m_epgHeader->epgSelectionTime();
    QDateTime serverTime    = m_mainWindow->serverTime();

    if ( scheduleList.size() > 0 )
    {
        for ( int i = 0; i < scheduleList.size(); i++ )
        {
            if ( scheduleList[ i ].time.toTime_t() + scheduleList[ i ].duration > visibleTime.toTime_t() )
            {
                if ( int( scheduleList[ i ].time.toTime_t() - visibleTime.toTime_t() ) / 60 * m_epgHeader->stretch() > epgRect.width() )
                {
                    break; // out of range
                }

                rect = epgRect;
                if ( scheduleList[ i ].time < visibleTime )
                {
                    rect.setWidth( ( scheduleList[ i ].time.toTime_t() - visibleTime.toTime_t() + scheduleList[ i ].duration ) / 60 * m_epgHeader->stretch() );
                }
                else
                {
                    rect.adjust( ( scheduleList[ i ].time.toTime_t() - visibleTime.toTime_t() ) / 60 * m_epgHeader->stretch(), 0, 0, 0 );
                    rect.setWidth( scheduleList[ i ].duration / 60 * m_epgHeader->stretch() );
                }

                bool highlight = false;
                if( option.state & QStyle::State_Selected )
                {
                    if ( ( selectionTime >= scheduleList[ i ].time ) || ( ( i == 0 ) && ( selectionTime < scheduleList[ i ].time ) ) )
                    {
                        if ( i < scheduleList.size() - 1 )
                        {
                            if ( selectionTime < scheduleList[ i + 1 ].time )
                            {
                                highlight = true;
                            }
                        }
                        else
                        {
                            highlight = true;
                        }
                    }
                }

                QBrush backGround;
                QColor origColor = painter->pen().color();
                QColor textColor = painter->pen().color();
                if( invertBackground )
                {
                    if ( highlight )
                    {
                        backGround = option.palette.highlight();
                        textColor = option.palette.highlightedText().color();
                    }
                    else
                    {
                        backGround = option.palette.midlight();
                    }
                }
                else
                {
                    if ( highlight )
                    {
                        backGround = option.palette.highlight();
                        textColor = option.palette.highlightedText().color();
                    }
                    else
                    {
                        backGround = option.palette.mid();
                    }
                }
                invertBackground = !invertBackground;

                painter->fillRect( rect, backGround );

                painter->drawLine( rect.topLeft(), rect.topRight() );
                rect.adjust( 4, 0, -8, 0 );
                painter->setPen( textColor );
                painter->drawText( rect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, scheduleList[ i ].title );

                painter->setPen( origColor );
            }
        }
        painter->drawLine( epgRect.x() + ( serverTime.toTime_t() - visibleTime.toTime_t() ) / 60 * m_epgHeader->stretch(),
                           epgRect.y(),
                           epgRect.x() + ( serverTime.toTime_t() - visibleTime.toTime_t() ) / 60 * m_epgHeader->stretch(),
                           epgRect.y() + 48  );
    }

    painter->restore();
}

QSize EpgDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    Q_UNUSED( option );
    Q_UNUSED( index );

    return QSize( 100, 48 );
}
